package net.wangds.i18n.utils;

import net.wangds.spring.utils.SpringUtils;
import net.wangds.log.helper.LogHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.DelegatingMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
 * 错误信息工具类.
 */
public final class I18nUtils {

    public static void addMessagePropertiesFile(String baseName){
        addMessagePropertiesFiles(baseName);
    }


    public static void addMessagePropertiesFiles(String... baseNames){

        Object obj= SpringUtils.getBean("messageSource");
        ResourceBundleMessageSource src = null;
        while(obj!=null) {
            if (obj instanceof DelegatingMessageSource) {
                DelegatingMessageSource dm = ((DelegatingMessageSource) obj);
                obj = ((DelegatingMessageSource) obj).getParentMessageSource();
            }else if(obj instanceof ResourceBundleMessageSource){
                src = SpringUtils.getBean("messageSource");
                break;
            } else {
                LogHelper.warn("messageSource type can't recognized:"+obj.getClass().getName());
                break;
            }
        }
        if(src!=null){
            src.addBasenames(baseNames);
        } else {
            LogHelper.info("can't add message properties file:"+ StringUtils.join(baseNames));
        }
    }

}
